package com.jiyi.activity.datainput.sm;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jiyi.bean.datainput.DataInputBaseBean;
import com.jiyi.bean.datainput.new_input.DetailBuildBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.AutoLinefeedLayout;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.RadioGroupUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/3/29.
 */

public class BuildDetailInfoReadActivity extends ActivityBase {
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.rbOne)
    RadioButton rbOne;
    @BindView(R.id.rbTwo)
    RadioButton rbTwo;
    @BindView(R.id.rbZhuZhai)
    RadioButton rbZhuZhai;
    @BindView(R.id.rbGongJian)
    RadioButton rbGongJian;
    @BindView(R.id.rgBuildCat)
    RadioGroup rgBuildCat;
    @BindView(R.id.tvHeight)
    TextView tvHeight;
    @BindView(R.id.tvDSCS)
    TextView tvDSCS;
    @BindView(R.id.tvDXCS)
    TextView tvDXCS;
    @BindView(R.id.tvDSMJ)
    TextView tvDSMJ;
    @BindView(R.id.tvDXMJ)
    TextView tvDXMJ;
    @BindView(R.id.tvUserFunc)
    TextView tvUserFunc;
    @BindView(R.id.tvBNCWZ)
    TextView tvBNCWZ;
    @BindView(R.id.allXFSS)
    AutoLinefeedLayout allXFSS;
    @BindView(R.id.tvOther)
    TextView tvOther;
    @BindView(R.id.tvControlLocal)
    TextView tvControlLocal;

    String build_id;

    final String[] items = new String[]{"自动喷水灭火系统", "火灾自动报警系统", "水喷雾灭火系统", "室内消火栓", "室外消火栓", "机械防烟设施", "机械排烟设施", "应急广播", "应急照明", "消防电梯"};
    LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_build_detail_info_read);
        ButterKnife.bind(this);
        inflater = LayoutInflater.from(this);
        init();
    }

    private void init()
    {
        getIntentData();
        setTop();
        setViewStatus();

        if (!TextUtils.isEmpty(build_id))
        {
            //修改时，获取数据
            getData();
        }
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("建筑详细情况");
        tvSearch.setVisibility(View.GONE);
    }

    private void setViewStatus() {
        RadioGroupUtil.disableRadioGroup(rgBuildCat);
    }

    private void getData() {
        HttpMethods.getInstance().getDetailBuildInfo(UserInfoUtil.getCurrentUserAccessToken(),build_id,2)
                .subscribe(new SampleProgressObserver<DetailBuildBean>(this) {
                    @Override
                    public void onNext(DetailBuildBean o) {
                        if (o == null || "".equals(o.getGson_null()))
                        {
                            o = new DetailBuildBean();
                            o.setCategory(1);
                            o.setCategory_o(1);
                            o.setHeight("");
                            o.setPlies_num("");
                            o.setUp_area("");
                            o.setDown_area("");
                            o.setUse_for("");
                            o.setRefuge_storey("");
//                            StringBuilder stringBuilder = new StringBuilder();
//                            for (int i = 0 ;i<items.length;i++)
//                            {
//                                stringBuilder.append("0").append(",");
//                            }
//                            o.setFighting_device(stringBuilder.toString().substring(0,stringBuilder.toString().length()-1));
                            o.setFighting_device("");
                            o.setO_fighting_device("");
                            o.setControl_room_area("");
                        }
                        fillData(o);
                    }
                });
    }

    private void fillData(DetailBuildBean detailBuildBean)
    {

        System.out.println("detailBuildBean.getCategory():"  +  detailBuildBean.getCategory());

        if (detailBuildBean.getCategory() == 0)
        {
            rbOne.setChecked(true);
        }
        else
        {
            rbTwo.setChecked(true);
        }

        System.out.println("detailBuildBean.getCategory_o():"  + detailBuildBean.getCategory_o());

        if (detailBuildBean.getCategory_o() == 0)
        {
            rbZhuZhai.setChecked(true);
        }
        else {
            rbGongJian.setChecked(true);
        }

        tvHeight.setText(detailBuildBean.getHeight());
        tvDSCS.setText(detailBuildBean.getUp_storey());
        tvDXCS.setText(detailBuildBean.getDown_storey());
        tvDSMJ.setText(detailBuildBean.getUp_area());
        tvDXMJ.setText(detailBuildBean.getDown_area());
        tvUserFunc.setText(detailBuildBean.getUse_for());
        tvBNCWZ.setText(detailBuildBean.getRefuge_storey());
        tvOther.setText(detailBuildBean.getO_fighting_device());
        tvControlLocal.setText(detailBuildBean.getControl_room_area());

//        String[] strStatus = detailBuildBean.getFighting_device().split(",");
//        boolean[] status = new boolean[strStatus.length];
//        for (int i=0;i<strStatus.length;i++)
//        {
//            status[i] = Integer.parseInt(strStatus[i]) == 0 ? false : true;
//        }
//        initSFSS(status);

        boolean[] status = new boolean[items.length];
        for (int i=0;i<items.length;i++)
        {
            status[i] = false;
        }
        if (!TextUtils.isEmpty(detailBuildBean.getFighting_device()))
        {
            String[] strStatus = detailBuildBean.getFighting_device().split(",");
            for (int i = 0;i<strStatus.length;i++)
            {
                for (int j = 0;j<items.length;j++)
                {
                    if (strStatus[i].equals(String.valueOf(j+1)))
                    {
                        status[j] = true;
                        break;
                    }
                }
            }
        }

        Log.e("status " ,status.toString());

        initSFSS(status);
    }

    private void initSFSS(final boolean[] status) {

        allXFSS.removeAllViews();
        Observable.range(0, 10)
                .map(new Function<Integer, DataInputBaseBean.ZYXFSS>() {
                    @Override
                    public DataInputBaseBean.ZYXFSS apply(Integer integer) throws Exception {
                        DataInputBaseBean.ZYXFSS zyxfss = new DataInputBaseBean.ZYXFSS();
                        zyxfss.setId(integer+1);
                        zyxfss.setName(items[integer]);
                        zyxfss.setCheck(status[integer]);
                        return zyxfss;
                    }
                })
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<DataInputBaseBean.ZYXFSS>() {
                    @Override
                    public void accept(DataInputBaseBean.ZYXFSS zyxfss) throws Exception {
                        View item = inflater.inflate(R.layout.item_text_checkbox, null, false);
                        item.setTag(zyxfss);
                        TextView tvName = item.findViewById(R.id.tvName);
                        tvName.setText(zyxfss.getName());
                        CheckBox cbXFSS = item.findViewById(R.id.checkBox);
                        cbXFSS.setChecked(zyxfss.isCheck());

                        item.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                DataInputBaseBean.ZYXFSS clickItem = (DataInputBaseBean.ZYXFSS) v.getTag();
                                CheckBox checkBox = v.findViewById(R.id.checkBox);
                                if (clickItem.isCheck()) {
                                    clickItem.setCheck(false);
                                    checkBox.setChecked(false);
                                } else {
                                    clickItem.setCheck(true);
                                    checkBox.setChecked(true);
                                }
                            }
                        });
                        allXFSS.addView(item);
                    }
                });

    }

    @OnClick({R.id.tvBack})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            default:
                break;
        }
    }
}
